/**
 * @author liuxin
 * @e-mail lxtobe5@gmail.com
 * @lasttime 2012.3.8
 */
package com.lx.utils
{
	import flash.utils.describeType;

	/**
	 * Class that contains static utility methods for manipulating and working with
	 * traversing instance property of any object,the Object object doesn't contain
	 * instance property.
	 */	
	public class TraverseObjectUtil
	{
		private static var _prevObj:Object;
		private static var _currObj:Object;
		private static var _flag:Boolean;
		private static var _xml:XML;
		
		/**
		 * If _currObj equal _prevObj absolutely, means its are the same object,
		 * don't need to analysis object again.
		 * @param obj    any object for preparing to analysis.
		 * @return true  when _currObj equal _prevObj absolutely or _prevObj is null.
		 *         false when _currObj unequal _prevObj absolutely.
		 */		
		private static function objectAbsoluteEqual(obj:Object):Boolean
		{
			if(_currObj)
				_prevObj = _currObj;
			
			_currObj = obj;
			
			if(!_prevObj)
				return true;
			return _prevObj === _currObj;
		}
		
		/**
		 * Produces an XML object that describes the ActionScript object named as the parameter 
		 * of the method.
		 * @param obj   any object for preparing to analysis.
		 * @return XML  if _flag is false,analysis object.
		 */		
		private static function analysisObject(obj:Object):XML
		{
			_flag = objectAbsoluteEqual(obj);
			
			if(_flag && _prevObj)
				return _xml;
			
			_xml = describeType(_currObj);
			return _xml;
		}
		
		/**
		 * @param xmllist  according to the name of property in XML,produces a xmllist
		 * @return Array   the value of property
		 */		
		private static function getArray(xmllist:XMLList):Array
		{
			var arr:Array = [];
			for each(var xml:XML in xmllist)
			{
				arr.push(xml.toXMLString());
			}
			arr.sort(Array.CASEINSENSITIVE);
			return arr;
		}
		
		/**
		 * @param obj    any object for preparing to analysis.
		 * @return Array the value of property
		 */		
		public static function getPropertyName(obj:Object):Array
		{
			analysisObject(obj);
			return getArray(_xml.accessor.@name);
		}
		
		
	}
}